package com.jlf.tool.validation.constraint;

import com.jlf.tool.validation.Mobile;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * 手机号码的校验
 *
 * @author wujr
 * 2021/1/13
 * @version 1.0
 * 变更历史
 * [wujr 2021/1/13 1.0 新增]
 */
public class MobileConstraintValidator implements ConstraintValidator<Mobile, String> {
    private Pattern mobilePattern;

    @Override
    public void initialize(Mobile mobile){
        this.mobilePattern = Pattern.compile(mobile.regexp());
    }
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return true;
        }
        return mobilePattern.matcher(value).matches();
    }
}
